package com.qf.zyh.web.serCont;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Emp;
import com.qf.zyh.services.EmpServ;
import com.qf.zyh.services.impl.EmpServImpl;
import com.qf.zyh.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/emp")
public class EmpCont  extends HttpServlet {
    EmpServ es = new EmpServImpl();

    public static void main(String[] args) {
        System.out.println();
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "checkCode"://验证码
                this.checkCode(req,resp);
                break;
            case "login"://登录
                this.login(req,resp);
                break;
            case "selectAll"://查询全部(已失效）
                this.selectAll(req,resp);
                break;
            case "updateeno"://根据eno查找需要修改的数据
                this.updateeno(req,resp);
                break;
            case "updateEmp"://修改
                this.updateEmp(req,resp);
                break;
            case "deleteEno"://删除
                this.deleteEno(req,resp);
                break;
            case "addEmp"://添加
                this.addEmp(req,resp);
                break;
            case "selectPage"://分页查询
                this.selectPage(req,resp);
                break;
        }
    }

    private void selectPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pi = req.getParameter("pageIndex");
        //验证获取的数据是否为空，为空设置为1，不为空转string为integr
        Integer i = pi == null ? 1 : Integer.valueOf(pi);
        Integer pageSize = 5;

        Map<String, Object> map = es.selectPage(i, pageSize);
        //存储map
        req.setAttribute("emp",map);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    private void addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String esex = request.getParameter("sex");
        Integer dno = Integer.valueOf(request.getParameter("dno").trim());
        Double salary = Double.valueOf(request.getParameter("salary"));
        Date date = DateUtil.stringtodate(request.getParameter("date"));
        Emp emp = new Emp(username,pwd,esex,salary,dno,date);
        System.out.println(emp);
        //进行新增操作
        boolean in = es.insert(emp);
        //获取编辑html
        String msg = "新增失败";
        if (in){
            msg = "新增成功";
        }
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("massage.jsp").forward(request,response);
    }

    private void deleteEno(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer eno =Integer.valueOf(request.getParameter("eno").trim());
        boolean de = es.delete(eno);
        String msg = "删除失败";
        if (de){
            msg = "删除成功";
        }
        //存储到msg中提交到通用页面，massage。jsp中
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("massage.jsp").forward(request,response);
    }

    private void updateEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer  eno =Integer.valueOf(request.getParameter("eno")) ;
        String ename = request.getParameter("username");
        String esex = request.getParameter("esex");
        Double salary =Double.valueOf(request.getParameter("salary"));
        String pwd = request.getParameter("pwd");
        Integer dno =Integer.valueOf(request.getParameter("dno")) ;
        Date bdate = DateUtil.stringtodate(request.getParameter("date")) ;

        Emp e = new Emp(eno,ename,esex,salary,pwd,dno,bdate);

        boolean up = es.update(e);
        //不用if else是因为必须先声明才能使用
        String msg = "更新失败";
        if (up){
            msg="更新成功";
        }
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("massage.jsp").forward(request,response);
    }

    private void updateeno(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("11");
        String num = req.getParameter("eno");
        System.out.println(num);
        Integer i = num == null ? 0 : Integer.valueOf(num);
        System.out.println(i);
        Emp emp = es.selectid(i);
        System.out.println(emp);
        if (emp!=null) {
            req.setAttribute("emp",emp);
            req.getRequestDispatcher("updateEmp.jsp").forward(req,resp);
        }else {
            resp.sendRedirect(req.getContextPath()+"/emp?method=selectPage");
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Emp> em = es.selectAll();
        req.setAttribute("emplist",em);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ename = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //传入数据进行登录验证
        Emp emp = es.selectin(ename, pwd);
        if (emp!=null){
            //登录成功则将获得数据存储到session中
            req.getSession().setAttribute(Const.IS_LOGIN,emp);
            resp.sendRedirect(req.getContextPath()+"/emp?method=selectPage");
        }else {
            req.setAttribute("mag","用户名密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }

    private void checkCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受前端传递数据
        String code = req.getParameter("code");
        //比较验证码
        System.out.println(code);
        String valiData = (String) req.getSession().getAttribute(Const.VALI_DATA);
        System.out.println(valiData);
        //equals相等，验证验证码相等
        if (valiData.equalsIgnoreCase(code)) {
            //写出到前端给正确还是错误
            resp.getWriter().write("{\"result\":true}");
        }else {
            resp.getWriter().write("{\"result\":false}");
        }
    }
}
